#include "facade.h"
#include "window.h"
#include "building.h"

#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/io/pcd_io.h>
#include <pcl/common/centroid.h>
#include <pcl/ModelCoefficients.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/common/centroid.h>
#include <pcl/common/transforms.h>
#include <pcl/common/transformation_from_correspondences.h>



void Facade::setParentBuilding(Building* b)
	{
		_building = b;

		std::string randomid;
		randomid.append("F");
		for(int i = 0; i < 10; i++)
		{
			int random = rand() % 100;
			std::ostringstream convert;
			convert << random;
			randomid.append(convert.str());
		}

		std::string id(b->getID() + "-" + randomid);
		_ID = id;
		//std::cout << "facade _ID = " << _ID << std::endl;
	}


cv::Mat* Facade::showResult(Eigen::Vector4f min_p, Eigen::Vector4f max_p, float res)
{
	int rows = ((max_p.y()-min_p.y()) / res) + 1;
	int cols = ((max_p.x()-min_p.x()) / res) + 1;

	cv::Mat *output = new cv::Mat(cv::Mat::zeros(rows, cols, CV_8UC3));


	std::cout << "draw Window Rectangles - start" << std::endl;
	for(int i = 0; i < _windows.size(); i++)
	{
		cv::Rect windowRectangle;
		
		Eigen::Vector2f* size = _windows[i]->getSize();
		Eigen::Vector2f* pos = _windows[i]->getPos();

		windowRectangle.width = int(size->x()/res + 0.5);
		windowRectangle.height = int(size->y()/res + 0.5);
		windowRectangle.x = int(pos->x()/res + 0.5);
		windowRectangle.y =  int(pos->y()/res + 0.5);

		//obere linke ecke auf untere linke korrigieren
		windowRectangle.y = rows - windowRectangle.y - windowRectangle.height;

		cv::rectangle( 
			*output,
			windowRectangle,
			cv::Scalar( 255, 100, 100 ),
			-1,
			8 );
	}


	std::cout << "draw Polygon - start" << std::endl;
	
	//std::vector<cv::Point*> points;

	int numVerts = _vertices.size();
	for(int i = 0; i < numVerts; i++)
	{
		
		Eigen::Vector2f* nexpoint;

		if(i == numVerts-1)
		{
			nexpoint = _vertices[0];
		}
		else
		{
			nexpoint = _vertices[i+1];
		}

		int startx = int(_vertices[i]->x()/res + 0.5);
		int starty = int(_vertices[i]->y()/res + 0.5);
		starty = rows - starty;
		int endx = int(nexpoint->x()/res + 0.5);
		int endy = int(nexpoint->y()/res + 0.5);
		endy = rows - endy;

		//cv::Point* thispoint = new cv::Point(startx,starty);
		//points.push_back(thispoint);

		cv::line( *output,
			cv::Point(startx,starty),
			cv::Point(endx,endy),
			cv::Scalar( 0, 255, 0 ),
			2,
			8);
	}

	/*
	std::cout << "cast" << std::endl;
	const cv::Point* verts[1] = { points[0] };
	int numberOfPoints = points.size();

	std::cout << "draw poly" << std::endl;
	cv::fillPoly (output, 
			verts, 
			&numberOfPoints, 
			1, 
			cv::Scalar (150, 150, 150), 
			8);
	*/


	//const char* name = "Facade Result";
	//imshow( name, output );
	return output;
	//cv::waitKey();
}